package aiku.numericalcontroloa.Controller.UserController;

import aiku.numericalcontroloa.Base.Utils.HttpStatus;
import aiku.numericalcontroloa.Base.Utils.Result;
import aiku.numericalcontroloa.Model.Dto.PageSelect;
import aiku.numericalcontroloa.Model.Dto.SysUserDto;
import aiku.numericalcontroloa.Model.Dto.UserDto;
import aiku.numericalcontroloa.Model.Dto.WxLoginDto;
import aiku.numericalcontroloa.Service.UserService;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Author 黄广庭
 * @Date 2024/1/6 17:07
 */
@RestController
@RequestMapping("/user")
@CrossOrigin("*")
@Slf4j
public class UserCont {

    private final UserService userService;

    public UserCont(UserService userService) {
        this.userService = userService;
    }

    /**
     * 登录
     * @param userDto 登录对象
     * @return 结果
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserDto userDto){
       return userService.login(userDto);
    }


    /**
     *退出登录
     * @return 结果
     */
    @GetMapping("logout")
    public Result logout(){
        StpUtil.logout();
        return  Result.success("退出登录成功");
    }

    /**
     * 添加用户
     * @param userDto 用户数据
     * @return 结果
     */
    @PostMapping("/addUser")
    public Result addUser(@RequestBody UserDto userDto){
        return userService.addUser(userDto);
    }

    /**
     * 修改用户
     * @param userDto 用户数据
     * @return 结果
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserDto userDto){
        return userService.updateUser(userDto);
    }

    /**
     * 删除用户
     * @param userDto
     * @return 结果
     */
    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestBody UserDto userDto){
        return userService.deleteUser(userDto);
    }

    /**
     * 获取用户
     * @param userDto 用户
     * @return 返回用户对象
     */
    @PostMapping("/getUser")
    public Result getUser(@RequestBody UserDto userDto){
        return userService.getUser(userDto.getId());
    }

    /**
     * 获取已删除的用户
     * @param userDto
     * @return
     */
    @PostMapping("/getUserWithDelete")
    public Result getUserWithDelete(@RequestBody UserDto userDto){
        return userService.getUserWithDelete(userDto.getId());
    }

    /**
     * 获取用户列表
     * @param pageSelect 分页
     * @return 返回用户集合
     */
    @PostMapping("/getUserList")
    public Result getUserList(@RequestBody PageSelect pageSelect){
        return userService.getUserList(pageSelect);
    }

    /**
     * 验证密码
     * @param userDto 用户数据
     * @return 结果
     */
    @PostMapping("/verifyPassword")
    public Result verifyPassword(@RequestBody UserDto userDto){
        return userService.verifyPassword(userDto);
    }

    /**
     * 修改密码
     * @param userDto 用户数据
     * @return 结果
     */
    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody UserDto userDto){
        return userService.changePassword(userDto);
    }

    /**
     * 重置密码
     * 重置的密码为该账号手机号后四位
     * @param userDto
     * @return
     */
    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestBody UserDto userDto) {
        return userService.resetPassword(userDto);
    }

    /**
     * 微信登录
     * @param wxLoginDto
     * @return
     * @throws IOException
     */
    @PostMapping("/wxLogin")
    public Result wxLogin(@RequestBody WxLoginDto wxLoginDto) throws IOException, JSONException {
        return userService.wxLogin(wxLoginDto);
    }

    /**
     * 绑定微信
     * @param wxLoginDto
     * @return
     */
    @PostMapping("/bindWx")
    public Result bindWx(@RequestBody WxLoginDto wxLoginDto) throws IOException, JSONException {
        return userService.bindWx(wxLoginDto);
    }

    /**
     * 管理员添加用户
     *
     */
    @PostMapping("/adminAddUser")
    public Result adminAddUser(@RequestBody SysUserDto sysUserDto){
        return userService.adminAddUser(sysUserDto);
    }

    /**
     * 管理员查询所有用户
     * @return 返回用户对象
     */
    @PostMapping("/adminGetUser")
    public Result adminGetUser(){
        return userService.adminGetUser();
    }

    /**
     * 管理查询所有业务员
     * @return 返回用户对象
     *
     */
    @PostMapping("/adminGetSalesman")
    public Result adminGetSalesman(){
        return userService.adminGetSalesman();
    }

    /**
     * 根据姓名工号模糊查询
     * @param userDto
     * @return
     */
    @PostMapping("/fuzzyQuery")
    public Result fuzzyQuery(@RequestBody UserDto userDto){
        return userService.fuzzyQuery(userDto);
    }

}
